package FileWriterTest;

import org.junit.Test;
import org.w3c.dom.ls.LSOutput;

import java.io.*;
import java.util.GregorianCalendar;

/**
 * @author 李泽伟
 */
public class MainTest {
    public static void main(String[] args) {
        FileReader reader = null;
        try {
            File file = new File("hello1.txt");
            reader = new FileReader(file);
            int data;
            while ((data = reader.read()) !=-1){
                System.out.print((char) data);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }finally {
            try {
                if (reader != null) {
                    reader.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @Test
    public void test(){
        FileReader fr = null;
        try {
            File file = new File("hello.txt");
            fr = new FileReader(file);
            char[] cBuffer = new char[10];
            int len;
            while ((len = fr.read(cBuffer))!=-1){
                //错误的写法，因长度限制，hello world 'ello worl'
//                for(char c:cBuffer){
//                    System.out.print(c);
//                }
//                System.out.println("****");
                //正确的写法
                for (int i = 0; i < len; i++) {
                    System.out.print(cBuffer[i]);
                }
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            if (fr != null) {
                try {
                    fr.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Test
    public void test2(){
        //数据写出
        //1.提供File类
        FileWriter writer = null;
        //2.提供FileWriter对象，用于数据写出
        try {
            File file = new File("WriteTest.txt");
            writer = new FileWriter(file,true);
            //3.写出操作
            writer.write("我爱江田清\n");
            writer.write("我爱田清");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }finally {
            //4.资源关闭
            try {
                if (writer != null) {
                    writer.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

   @Test
   public void test3() {
       //尝试使用字符流复制一张图片（不能使用字符流处理图片这种字节流二进制数据文件的复制，但是可以用于复制字符流文件）
       FileReader srcReader = null;
       FileWriter desWriter = null;
       //1.File类对象实例化
       File srcFile = new File("test.png");
       File desFile = new File("testCopy.png");
       try {
           //2.FileReader FileWriter对象实例化
           srcReader = new FileReader(srcFile);
           desWriter = new FileWriter(desFile);
           //3.操作文件
           char[] cBuffer = new char[10];
           int len;
           while ((len = srcReader.read(cBuffer)) !=-1){
               desWriter.write(cBuffer, 0, len);
               System.out.println(len);
           }
       } catch (IOException e) {
           throw new RuntimeException(e);
       }finally {
           //关闭文件
           try {
               if (srcReader != null) {
                   srcReader.close();
               }
           } catch (IOException e) {
               e.printStackTrace();
           }
           try {
               if (desWriter != null) {
                   desWriter.close();
               }
           } catch (IOException e) {
               e.printStackTrace();
           }
       }
   }
   @Test
   public void test1(){
        //使用字节流复制一张图片(以下代码是正确的，成功复制字节流文件)
       FileInputStream srcStream = null;
       FileOutputStream desStream = null;
       File srcFile = new File("test.png");
       File desFile = new File("testCopyByte.png");
       try {
           srcStream = new FileInputStream(srcFile);
           desStream = new FileOutputStream(desFile);
           byte[] bytes = new byte[10];
           int len;
           while ((len = srcStream.read(bytes))!=-1){
               desStream.write(bytes, 0, len);
               System.out.println(len);
           }
           System.out.println("图片文件复制成功");
       } catch (IOException e) {
           throw new RuntimeException(e);
       } finally {
           try {
               if (srcStream != null) {
                   srcStream.close();
               }
           } catch (IOException e) {
               e.printStackTrace();
           }
           try {
               if (desStream != null) {
                   desStream.close();
               }
           } catch (IOException e) {
               e.printStackTrace();
           }
       }
   }
}
